package components.photo
{
	import UIFree.ObjectEvent;
	import UIFree.uif_component;
	
	import UIPhoto.uip_events;
	import UIPhoto.uip_photoEditor;
	import UIPhoto.uip_rect;
	
	import UIUtils.uif_matrix;
	
	import classes.clt_bitmap;
	
	import flash.display.Bitmap;
	import flash.display.BitmapData;
	import flash.display.BlendMode;
	import flash.display.DisplayObject;
	import flash.events.Event;
	import flash.events.MouseEvent;
	import flash.filters.BlurFilter;
	import flash.filters.GlowFilter;
	import flash.geom.Point;
	import flash.geom.Rectangle;

	public class clt_rect extends uip_rect {
		private var _mask		: clt_bitmap;
		private var _alphaMask	: clt_bitmap;
		private var _blur		: BlurFilter;
		
		public function clt_rect(parent:uip_photoEditor, images:Object) {
			super(parent, images);
			_blur = new BlurFilter(0, 0, 2);
			_alphaMask = new clt_bitmap();
			maskBlur = 7;
		}
		
		override protected function minSize(): Number {
			return image.size.length * 0.5;
		}
		
		public function get maskBlur(): Number {
			return _blur.blurX;
		}
		
		public function set maskBlur(a_blur: Number): void {
			_blur.blurX = a_blur;
			_blur.blurY = a_blur;
			_alphaMask.filters = [_blur];
		}
		
		public function assignMask(a_mask: DisplayObject): void {
			if (a_mask && a_mask.width) {
				if (_alphaMask.bitmapData) _alphaMask.bitmapData.dispose();
				
				_alphaMask.bitmapData = new BitmapData(a_mask.width, a_mask.height);
				_alphaMask.asAlpha(a_mask, true);
				_alphaMask.smoothing = true;
				
				var tmp: clt_bitmap = new clt_bitmap();
				tmp.copy(a_mask);
				if (_mask) _mask.bitmapData.dispose();
				else {
					_mask = new clt_bitmap();
					_mask.filters = [
						new GlowFilter(0x00FF00, 1, 3, 3, 3, 1, false, true)
					];
					addChild(_mask);
				}
				_mask.bitmapData = new BitmapData(a_mask.width, a_mask.height);
				_mask.asAlpha(a_mask);
				updateMaskSize();
				setPosNotify();
			}
		}
		
		protected function updateMaskSize(): void {
			if (_mask) {
				var rect: Rectangle = EnterHere(getMaskSize());
				_mask.x = rect.x;
				_mask.y = rect.y;
				_mask.width = rect.width;
				_mask.height = rect.height;
			}
		}
		
		protected function getMaskMatrix(): uif_matrix {
			var mat		: uif_matrix 	= new uif_matrix();
			return mat;
		}
		
		public function getMaskSize(): Point {
			return _mask?(new Point(_mask.bitmapData.width, _mask.bitmapData.height)):null;
		}
		
		override protected function setPosNotify(): void {
			if (_mask) {
				parent.dispatchEvent(new ObjectEvent(uip_events.UPDATEMATRIX, {
					matrix		: getPosMatrix(),
					maskSize	: getMaskSize(),
					maskMatrix	: getMaskMatrix()
				}));
			}
		}
		
		public function get clipBitmap(): BitmapData {
			var msize: Point = getMaskSize();
			var clipRect: Rectangle = new Rectangle(0, 0, msize.x, msize.y);
			var a_tmp: BitmapData = new BitmapData(msize.x, msize.y, true, 0);
			
			clipRect.inflate(-maskBlur, -maskBlur);
			
//			a_tmp.bitmapData.fillRect(a_tmp.bitmapData.rect, 0xFFFFFFFF);
			a_tmp.draw(photo, getPosMatrix(), null, null, clipRect);
			a_tmp.draw(_alphaMask, getMaskMatrix(), null, BlendMode.ERASE, clipRect);
			return a_tmp;
		}
		
		override public function dispose():void {
			_mask.bitmapData.dispose();
			_alphaMask.bitmapData.dispose();
			super.dispose();
		}
		
		override protected function updateBounds(): uif_component {
			var result: uif_component = super.updateBounds();
			updateMaskSize();
			return result;
		}

		override public function restore(a_restore: Object): void {
			if (_mask && a_restore.height) {
				a_restore.height = a_restore.width * _mask.height / _mask.width
			}
			super.restore(a_restore);
		}
	}
}